<template>
	<view class="page">
		<view class="page_con">
			<view class="slogen_item">
				<image src="@/static/img/login/login_slogen.png" mode="aspectFit"></image>
			</view>
			<view class="num_section">
				<view class="">
					您的可用印章数量
				</view>
				<text>{{num}}</text>
				<view class="">
					枚
				</view>
			</view>
			<view class="activity_desc" @click="openWeb('/pages/home/activityDesc?type=4')">
				活<br>动<br>说<br>明
			</view>
			<view class="list_section">
				<view class="list_con">
					<image class="active1" src="@/static/img/exchange/active1.png" mode=""></image>
					<view class="list_title">
						兑换扭蛋券(线下)
					</view>
					<view class="min_title">
						请选择兑换券的数量
					</view>
					<view class="min2_title">
						（说明：6枚章=1张扭蛋券）
					</view>
					<view class="lits_item">
						<view class="but_minus" @click="changeMinus()">
							-
						</view>
						<input class="" type="number" v-model="nums">
						<view class="but_add" @click="changeAdd()">
							+
						</view>
					</view>
					<view class="butt_submit">
						<view class="but" @click="check">
							立即兑换
						</view>
					</view>
				</view>
			</view>
			<view class="banner">
				<image src="@/static/img/exchange/banner.png" mode="aspectFill"></image>
			</view>

			<!-- 兑换成功弹窗 -->
			<uni-popup ref="popup1" background-color="transparent" mask-background-color="rgba(0,0,0,0.8)">
				<view class="popup-content1">
					<image class="img1" src="@/static/img/exchange/popup_image3.png" mode=""></image>
					<image class="img2" src="@/static/img/exchange/popup_image1.png" mode=""></image>
					<view class="butt_submit">
						<view class="but" @click="closePopup1">
							知道了
						</view>
					</view>
				</view>
			</uni-popup>
			<!-- 兑换失败弹窗 -->
			<uni-popup ref="popup2" background-color="transparent" mask-background-color="rgba(0,0,0,0.8)">
				<view class="popup-content2">
					<!-- <image class="img1" src="@/static/img/exchange/popup_image4.png" mode=""></image> -->
					<view class="resCode">
						{{msg}}
					</view>
					<image class="img2" src="@/static/img/exchange/popup_image2.png" mode=""></image>
					<view class="butt_submit">
						<image src="@/static/img/exchange/close.png" mode="" @click="closePopup2"></image>
					</view>
				</view>
			</uni-popup>

			<!-- 兑换提醒弹窗 -->
			<uni-popup ref="popup3" background-color="transparent" mask-background-color="rgba(0,0,0,0.8)">
				<view class="content">

					<view class="popup-content3">
						<image class="top_img" src="@/static/img/my/onLine.png" mode=""></image>
						<view class="title">
							扭蛋券兑换说明
						</view>
						<view class="desc">
							请注意! 扭蛋券仅限本人亲至碧波庭上海展厅现场使用。兑换后不支持退兑，请勾选确认兑换代表您已知晓。
						</view>
						<checkbox-group @change="checkboxChange">
							<checkbox value="cb" :checked="isRead" /><text>我已知晓，确认兑换</text>
						</checkbox-group>
						<view class="sub_item">
							<view class="but" v-if="isRead" @click="submit">
								确定
							</view>
							<view class="but_disable" v-else>
								确定
							</view>
						</view>
					</view>
					<image class="close_img" src="@/static/img/exchange/close.png" @click="closePopup3" mode=""></image>
				</view>
			</uni-popup>
		</view>
		<!-- 底部tabBar -->
		<tabbar current="0"></tabbar>
	</view>
</template>

<script>
	import {
		setBadgeEggsExchange,
		getBadgeUnusedCount
	} from '@/api/api.js'
	import tabbar from '@/components/tabBar.vue' //自定义tabBar
	export default {
		data() {
			return {
				num: 0,
				nums: 1,
				msg: "系统错误",
				isRead: false
			}
		},
		components: {
			tabbar,
		},
		onLoad(e) {
			this.getCount()
		},
		onShow() {},
		onHide() {},
		methods: {
			checkboxChange(v) {
				if (v.detail.value.length > 0) {
					this.isRead = true
				} else {
					this.isRead = false
				}
			},
			check() {
				this.$refs.popup3.open("center")
				this.isRead = false
			},
			// 获取可用印章数量
			getCount() {
				getBadgeUnusedCount().then(res => {
					if (res.data) {
						this.num = res.data
					} else {
						this.num = 0
					}
				})
			},
			// 关闭弹窗
			closePopup3() {
				this.isRead = false
				this.$refs.popup3.close()
			},
			// 关闭弹窗
			closePopup2() {
				this.$refs.popup2.close()
			},
			// 关闭弹窗
			closePopup1() {
				this.$refs.popup1.close()
			},
			// 去兑换
			submit() {
				this.$refs.popup3.close()
				setBadgeEggsExchange({
					num: this.nums
				}).then(res => {
					if (res.code === 0) {
						this.$refs.popup1.open("center")
						this.getCount()
					} else {
						this.msg = res.msg
						this.$refs.popup2.open("center")
					}
				})
			},
			openWeb(v) {
				uni.navigateTo({
					url: v
				})
			},
			// 减
			changeMinus() {
				if (this.nums == 1) {
					this.nums = 1
					return
				}
				this.nums--
			},
			// 加
			changeAdd() {
				if (this.nums >= this.num) {
					uni.showToast({
						icon: 'none',
						title: `印章数量不够哦~`
					})
					return
				}
				this.nums++
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding-bottom: 120rpx;
	}

	.page_con {
		min-height: calc(100vh - 116rpx);
		min-height: calc(100dvh - 116rpx);
		background-image: url('@/static/img/index/index_bakc.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;
		background-position: top center;
		position: relative;
		padding-bottom: 80rpx;

		.slogen_item {
			text-align: center;

			image {
				margin-top: 12%;
				width: 619rpx;
				height: 260rpx;
			}
		}

		.num_section {
			margin-top: 40rpx;
			text-align: center;
			font-size: 30rpx;
			color: #1F6DC1;
			height: 70rpx;
			line-height: 70rpx;
			background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, .8), rgba(255, 255, 255, 0));
			display: flex;
			justify-content: center;
			font-weight: 600;

			text {
				border-radius: 50%;
				text-align: center;
				color: #fff;
				height: 62rpx;
				line-height: 62rpx;
				width: 62rpx;
				background-color: #1F6DC1;
				margin: 4rpx 12rpx 0;
				display: inline-block;
				font-size: 48rpx;

			}
		}

		.activity_desc {
			position: absolute;
			right: 0;
			top: 130rpx;
			background: #26292C;
			border-radius: 8rpx 0rpx 0rpx 8rpx;
			opacity: 0.61;
			font-size: 28rpx;
			color: #FFFFFF;
			padding: 12rpx;
		}

		.list_section {
			padding: 0 20rpx;
			margin-top: 40rpx;

			.list_con {
				background: #FFFAF1;
				border-radius: 32rpx;
				padding: 0 40rpx 40rpx;
				position: relative;

				.active1 {
					width: 109rpx;
					height: 109rpx;
					position: absolute;
					left: 0;
					top: 0;
				}

				.list_title {
					width: 365rpx;
					height: 75rpx;
					line-height: 75rpx;
					background: linear-gradient(90deg, #EDD08A, #BD8229);
					border-radius: 0rpx 0rpx 32rpx 32rpx;
					font-size: 32rpx;
					color: #FFFFFF;
					margin: 0 auto;
					text-align: center;
				}

				.min_title {
					font-size: 28rpx;
					color: #000000;
					text-align: center;
					margin-top: 100rpx;
					font-weight: bold;
				}

				.min2_title {
					text-align: center;
					font-size: 24rpx;
					color: red;
					margin-top: 20rpx;

				}

				.lits_item {
					display: flex;
					justify-content: center;
					align-items: center;
					flex-wrap: wrap;
					padding: 60rpx 0rpx 30rpx;
					text-align: center;

					.but_minus {
						background: #F5F5F5;
						border-radius: 8rpx;
						border: 1px solid #E8E8E8;
						width: 90rpx;
						height: 65rpx;
						font-size: 40rpx;
						color: #777777;
					}

					input {
						background: #FFFFFF;
						border-radius: 8rpx;
						border: 1px solid #E8E8E8;
						width: 175rpx;
						height: 65rpx;
						margin: 0 2rpx;
					}

					.but_add {
						width: 90rpx;
						height: 65rpx;
						background: #F5F5F5;
						border-radius: 8rpx;
						border: 1px solid #E8E8E8;
						font-size: 40rpx;
						color: #777777;
					}

				}

				.page_pagination {
					width: 80%;
					margin: 0 auto;
				}


			}
		}

		.butt_submit {
			text-align: center;
			margin-top: 50rpx;

			.but {
				margin: 0 auto;
				width: 350rpx;
				height: 129rpx;
				background-image: url("@/static/img/exchange/butt_back.png");
				font-size: 36rpx;
				background-size: 100% 100%;
				background-repeat: no-repeat;
				background-position: center;
				color: #FFFFFF;
				text-align: center;
				padding-top: 30rpx;
				font-weight: bold;

			}
		}

		.banner {
			margin-top: 40rpx;
			text-align: center;

			image {
				width: 702rpx;
				height: 275rpx;
			}
		}

		.popup-content1 {
			text-align: center;

			.img1 {
				width: 569rpx;
				height: 85rpx;
			}

			.img2 {
				width: 286rpx;
				height: 355rpx;
			}
		}

		.popup-content2 {
			text-align: center;

			.img1 {
				width: 569rpx;
				height: 85rpx;
			}

			.img2 {
				width: 288rpx;
				height: 288rpx;
			}

			.resCode {
				color: #fff;
				font-weight: bold;
				margin-bottom: 40rpx;
			}

			.butt_submit {
				image {
					width: 65rpx;
					height: 65rpx;
				}
			}
		}
	}

	.content {
		text-align: center;

		.popup-content3 {
			background: #FFFFFF;
			border-radius: 15rpx;
			position: relative;
			text-align: center;
			padding-top: 130rpx;
			width: 80%;
			margin: 0 auto;
			padding-bottom: 30rpx;

			.top_img {
				width: 188rpx;
				height: 188rpx;
				position: absolute;
				top: -99rpx;
				left: 0;
				right: 0;
				margin: auto;
			}

			.title {
				font-weight: bold;
				font-size: 36rpx;
				color: #004EA5;
			}

			.desc {
				font-size: 32rpx;
				color: #262626;
				line-height: 48rpx;
				padding: 56rpx 60rpx;
				text-indent: 2em; //缩进
				text-align: left;
			}

			checkbox-group {
				display: flex;
				align-items: center;
				justify-content: center;

				text {
					font-size: 32rpx;
					color: #3793FF;
				}
			}

			.sub_item {
				.but {
					width: 80%;
					margin: 65rpx auto 0rpx;
					background: #3489FF;
					border-radius: 40rpx;
					color: #fff;
					padding: 23rpx 0;
				}

				.but_disable {
					width: 80%;
					margin: 65rpx auto 0rpx;
					background: #DFDFDF;
					border-radius: 40rpx;
					color: #5E5D5D;
					padding: 23rpx 0;
				}
			}
		}

		.close_img {
			width: 65rpx;
			height: 65rpx;
			margin-top: 50rpx;
			margin: 50rpx auto 0;
		}
	}
</style>
